﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.UI.Grid;
using NCKH.Controllers;
using NCKH.models;
using NCKH.View.ThongKe;
using System.Linq;
using System.Text.RegularExpressions;


namespace NCKH.View.XepLichThi.KhoiTao
{
    public partial class frmTaoDanhSachThi : DevExpress.XtraEditors.XtraForm
    {
        public static int ID_mons { get; set; }
        public static string TenMons { get; set; }
        public frmTaoDanhSachThi()
        {
            InitializeComponent();
        }

        private void frmTaoDanhSachThi_Load(object sender, EventArgs e)
        {
            //cmbHe.Items.Add("Đại học chính quy");
            //cmbHe.Items.Add("Cao đẳng chính quy");
            gridView1.Config(true, false, false, true, false, false, false, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            gridView2.Config(true, false, false, false, false, false, false, false, false);
            gridView2.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            gridView1.AppearanceColumns(new[] { colTen_mon });
            gridView1.GroupColumns(new[] { colTen_khoa }, true);
            viewTaoDanhSachThongTinMonHocDK1BindingSource.DataSource = (new Context()).View_ThongTinLopTinChi_1s.ToList();
            
            PLAN_HocKyDangKy_TCCtrl.LoadDataSource(cmbNamHoc);
            //Context db = new Context();
            //var abc = db.View_DanhSachSVLopTinChi_1s.Where(a => a.Ten_mon == TenMons && a.Nam_hoc == cmbNamHoc.Text && a.Hoc_ky == (cmbHocKy.Text == "1" ? 1 : 2) && a.Ten_he == cmbHe.Text && a.Dot == int.Parse(cmbDot.Text)).Distinct().ToList();
            //viewDanhSachSVLopTinChi1BindingSource.DataSource = abc.ToList();
            
        }

        private void cmbNamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            PLAN_HocKyDangKy_TCCtrl.LoadDataSource(cmbHocKy, cmbNamHoc.Text);
          
        }

        private void cmbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            PLAN_HocKyDangKy_TCCtrl.LoadDataSource(cmbDot, cmbNamHoc.Text, cmbHocKy.Text);
           
        }

        private void cmbDot_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDot.Text == "1" && cmbHocKy.Text == "1A")
                chkKyPhu.Checked = true;
            else
                chkKyPhu.Checked = false;
            PLAN_HocKyDangKy_TCCtrl.LoadDataSource(cmbHe, cmbNamHoc.Text, cmbHocKy.Text, cmbDot.Text);

          

        }
        List<View_TaoDanhSach_ThongTinMonHocDK_1> Thongtindangkimonhoc { get; set; }
        List<View_TaoDanhSach_ThongTinMonHocDK_1> Thongtindangkimonhoc1 { get; set; }
        List<EXT_View_DanhSachSVLopTinChi_1> DanhSachLopTinChi { get; set; }
        private void cmbHe_SelectedIndexChanged(object sender, EventArgs e)
        {
            Context db = new Context();
            if (Thongtindangkimonhoc == null)
                Thongtindangkimonhoc = db.View_TaoDanhSach_ThongTinMonHocDK_1s.ToList();
            Thongtindangkimonhoc1 = Thongtindangkimonhoc.Where(a => a.Nam_hoc == cmbNamHoc.Text && a.Hoc_ky == (cmbHocKy.Text == "2" ? 2 : 1) && a.Ten_he == cmbHe.Text && a.Dot == int.Parse(cmbDot.Text)).ToList();
            gridControl1.BeginUpdate();
            gridControl1.DataSource = Thongtindangkimonhoc1.ToList();
            gridControl1.RefreshDataSource();
            gridControl1.EndUpdate();

        }

        private void txtTimTenHocPhan_TextChanged(object sender, EventArgs e)
        {
            Context db = new Context();
            var a = from p in Thongtindangkimonhoc1
                    where p.ID_mon.ToString().Contains(txtTimTenHocPhan.Text)
                    select p;
            gridControl1.BeginUpdate();
            gridControl1.DataSource = a.ToList();
            gridControl1.RefreshDataSource();
            gridControl1.EndUpdate();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            int abc = 0;
            var row = gridView1.GetFocusedRow() as View_TaoDanhSach_ThongTinMonHocDK_1;
            txtTenHocPhan.Text = row.Ten_mon;
            TenMons = row.Ten_mon;
            //EXT_View_DanhSachSVLopTinChi_1_Ctrl.LoadData(row.ID_mon, cmbNamHoc.Text, (cmbHocKy.Text == "1" ? 1 : 2), int.Parse(cmbDot.Text), cmbHe.Text);
            ID_mons = row.ID_mon;
            
            Context db = new Context();
            var values = db.View_DanhSachSVLopTinChi_1s.Where(p => p.ID_mon == ID_mons && p.Nam_hoc == cmbNamHoc.Text && p.Dot == int.Parse(cmbDot.Text) && p.Hoc_ky == (cmbHocKy.Text == "2" ? 2 : 1) && p.ID_he == (cmbHe.Text == "Đại học chính quy" ? 1 : 2)).Distinct().ToList();

            var result = (from p in values
                          select new EXT_View_DanhSachSVLopTinChi_1
                          {

                              Trang_thai = false,
                              Ngaysinh = (DateTime)p.Ngay_sinh,
                              Ho_ten = p.Ho_ten,
                              Ma_sv = p.Ma_sv
                          }).ToList();
            DanhSachLopTinChi = result.ToList();
            // result.ForEach(f => { f.Trang_thai = true; });
            foreach (var value1 in DanhSachLopTinChi)
            {
                //value1.Trang_thai = true;
                if (chkSinhVienMTC.Checked)
                {

                    int value2 = values.Where(p => p.Ma_sv == value1.Ma_sv).Select(a => a.ID_sv).First();
                    var value3 = db.MARK_Diem_TCs.Where(p => p.ID_sv == value2 && p.ID_mon == ID_mons ).Select(a => a.ID_diem).ToList();
                    if (value3.Any())
                    {
                        var value4 = db.MARK_DiemThanhPhan_TCs.Where(p => p.ID_diem == value3.First() && p.Hoc_ky_TP == (cmbHocKy.Text == "2" ? 2 : 1) && p.Nam_hoc_TP == cmbNamHoc.Text).Select(a => a.Diem).ToList();
                        if (value4.Any() && value4.First()<4)
                        {
                            value1.Trang_thai = true;
                            abc++;
                        }
                    }
                    else
                    {
                        value1.Trang_thai = true;
                        abc++;
                    }
                }

            }
            gridControl2.BeginUpdate();
            gridControl2.DataSource = DanhSachLopTinChi.ToList();
            gridControl2.RefreshDataSource();
            gridControl2.EndUpdate();
            lblTongSoSV.Text = DanhSachLopTinChi.Count().ToString();
            txtMTC.Text = abc.ToString();
        }

        private void chkSinhVienMTC_CheckedChanged(object sender, EventArgs e)
        {
            if (TenMons != null)
            {
                int abc = 0;
                Context db = new Context();
                var values = db.View_DanhSachSVLopTinChi_1s.Where(p => p.ID_mon == ID_mons && p.Nam_hoc == cmbNamHoc.Text && p.Dot == int.Parse(cmbDot.Text) && p.Hoc_ky == (cmbHocKy.Text == "2" ? 2 : 1) && p.ID_he == (cmbHe.Text == "Đại học chính quy" ? 1 : 2)).Distinct().ToList();
                
                var result = (from p in values
                              select new EXT_View_DanhSachSVLopTinChi_1
                              {

                                  Trang_thai = false,
                                  Ngaysinh = (DateTime)p.Ngay_sinh,
                                  Ho_ten = p.Ho_ten,
                                  Ma_sv = p.Ma_sv
                              }).ToList();
                DanhSachLopTinChi = result.ToList();
                // result.ForEach(f => { f.Trang_thai = true; });
                foreach (var value1 in DanhSachLopTinChi)
                {
                    //value1.Trang_thai = true;
                    if (chkSinhVienMTC.Checked)
                    {

                        int value2 = values.Where(p => p.Ma_sv == value1.Ma_sv).Select(a => a.ID_sv).First();
                        var value3 = db.MARK_Diem_TCs.Where(p => p.ID_sv == value2 && p.ID_mon == ID_mons).Select(a => a.ID_diem).ToList();
                        if (value3.Any())
                        {
                            var value4 = db.MARK_DiemThanhPhan_TCs.Where(p => p.ID_diem == value3.First() && p.Hoc_ky_TP == (cmbHocKy.Text == "2" ? 2 : 1) && p.Nam_hoc_TP == cmbNamHoc.Text).Select(a => a.Diem).ToList();
                            if (value4.Any() && value4.First() < 4)
                            {
                                value1.Trang_thai = true;
                                abc++;
                            }
                        }
                        else
                        {
                            value1.Trang_thai = true;
                            abc++;
                        }
                    }

                }
                gridControl2.BeginUpdate();
                gridControl2.DataSource = DanhSachLopTinChi.ToList();
                gridControl2.RefreshDataSource();
                gridControl2.EndUpdate();
                lblTongSoSV.Text = DanhSachLopTinChi.Count().ToString();
                txtMTC.Text = abc.ToString();
            }
        }

        private void btnTaoDanhSach_Click(object sender, EventArgs e)
        {
            Context db = new Context();
            var values = db.EXM_ToChucThiPhongs.Where(p => p.ID_mon == ID_mons).Select(a=>a.ID_mon).FirstOrDefault();
            if (values != 0)
            {
                MessageBox.Show("Môn học đã được tạo trong kỳ này", "Chú ý");
            }
            else
            {
                var Ky_dang_kys = db.PLAN_HocKyDangKy_TCs.Where(p => p.Nam_hoc == cmbNamHoc.Text && p.Hoc_ky == (cmbHocKy.Text == "2" ? 2 : 1) && p.Dot == int.Parse(cmbDot.Text)).Select(a => a.Ky_dang_ky).First();
                var dsltc = DanhSachLopTinChi.Where(p => p.Trang_thai == false).ToList();
                int Sosvdsltc=dsltc.Count();
                int So1 = int.Parse(txtSoSVtui.Text);
                int So2 = Sosvdsltc / So1;
                int So3 = Sosvdsltc % So1;
                int So_bao_danh = 1;
                for (int i = 0; i < So2+1; i++)
                {
                    int So_sv=So1;
                    if (i == So2)
                        So_sv = So3;
                    var phongthi = new EXM_ToChucThiPhong
                    {
                        Sosv = So_sv,
                        ID_mon = ID_mons,
                        Ky_dang_ky=Ky_dang_kys,
                        Hinh_thuc_thi=false
                    };
                    db.EXM_ToChucThiPhongs.InsertOnSubmit(phongthi);
                    db.SubmitChanges();
                    int max = db.EXM_ToChucThiPhongs.Select(p => p.ID_phong_thi_1).Max();
                    int bien = So_bao_danh-1;
                    foreach (var value in dsltc.Skip(bien).Take(So_sv))
                    {
                        int ID_svs = db.STU_HoSoSinhViens.Where(p => p.Ma_sv == value.Ma_sv).Select(a => a.ID_sv).First();
                        var danhsachphongthi = new EXM_DanhSachPhongThi
                        {
                            ID_sv = ID_svs,
                            SBD=So_bao_danh,
                            ID_phong_thi_1=max
                        };
                        So_bao_danh++; 
                        db.EXM_DanhSachPhongThis.InsertOnSubmit(danhsachphongthi);
                        db.SubmitChanges();
                    }
                }
                
                MessageBox.Show("Tạo thành công", "Chú ý");
            }
        }

    }
}